﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ProgNetComponents.Design.Attributes;
using System.ComponentModel;
using DevExpress.XtraEditors.Controls;

namespace ProgNetDxComponents.Controls.Editors
{
    public class MonthCheckedListBox : Control
    {
        private CheckedListBoxControl listBox;
        private Months _months;
        [System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content)]
        public Months Months
        {
            get
            {
                return _months;
            }
        }
        public bool CheckOnClick
        {
            get { return listBox.CheckOnClick; }
            set { listBox.CheckOnClick = value; }
        }
        public int[] GetCheckedMonths()
        {
            List<int> list = new List<int>();
            foreach (CheckedListBoxItem i in listBox.Items)
                if (i.CheckState == CheckState.Checked)
                    list.Add(i.Value.AsInt());
            return list.ToArray();
        }

        public MonthCheckedListBox()
        {
            InitializeComponent();
        }
        private void InitializeComponent()
        {
            _months = new Editors.Months();
            _months.MonthChanged += new EventHandler<MonthCheckedChangedEventArgs>(_months_MonthChanged);
            listBox = new CheckedListBoxControl();
            listBox.SortOrder = SortOrder.Ascending;
            listBox.Dock = DockStyle.Fill;
            listBox.MultiColumn = true;
            listBox.CheckOnClick = true;
            for (int i=1; i < 13; i++)
                _months_MonthChanged(Months, new MonthCheckedChangedEventArgs(i, true));
            this.Size = listBox.Size;
            this.Controls.Add(listBox);
        }

        void _months_MonthChanged(object sender, MonthCheckedChangedEventArgs e)
        {
            if (e.Checked)
            {
                foreach (CheckedListBoxItem i in listBox.Items)
                {
                    if (i.Value.AsInt() == e.Month)
                        return;
                }
                listBox.Items.Add(new CheckedListBoxItem(e.Month, System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(e.Month), CheckState.Checked));
            }
            else
            {
                CheckedListBoxItem rem = null;
                foreach (CheckedListBoxItem i in listBox.Items)
                {
                    if (i.Value.AsInt() == e.Month)
                    {
                        rem = i;
                        break;
                    }
                }
                if (rem != null)
                    listBox.Items.Remove(rem);
            }
        }
        public void MarkMonth(int month, bool mark)
        {
            CheckedListBoxItem itm = null;
            foreach (CheckedListBoxItem i in listBox.Items)
                if (i.Value.AsInt() == month)
                {
                    itm = i;
                    break;
                }
            if (itm != null)
                itm.CheckState = mark ? CheckState.Checked : CheckState.Unchecked;
        }
    }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class Months : ProgNetComponents.Components.CustomTypeDescriptorObject
    {
        internal event EventHandler<MonthCheckedChangedEventArgs> MonthChanged;

        internal void OnMonthChanged(int month, bool Checked)
        {
            if (MonthChanged != null)
                MonthChanged(this, new MonthCheckedChangedEventArgs(month, Checked));
        }

        private bool _January = true;

        [MonthDisplayName(1)]
        public bool January
        {
            get { return _January; }
            set { _January = value; OnMonthChanged(1, value); }
        }

        private bool _February = true;

        [MonthDisplayName(2)]
        public bool February
        {
            get { return _February; }
            set { _February = value; OnMonthChanged(2, value); }
        }

        private bool _March = true;

        [MonthDisplayName(3)]
        public bool March
        {
            get { return _March; }
            set { _March = value; OnMonthChanged(3, value); }
        }

        private bool _April = true;

        [MonthDisplayName(4)]
        public bool April
        {
            get { return _April; }
            set { _April = value; OnMonthChanged(4, value); }
        }

        private bool _May = true;

        [MonthDisplayName(5)]
        public bool May
        {
            get { return _May; }
            set { _May = value; OnMonthChanged(5, value); }
        }

        private bool _June = true;

        [MonthDisplayName(6)]
        public bool June
        {
            get { return _June; }
            set { _June = value; OnMonthChanged(6, value); }
        }

        private bool _July = true;

        [MonthDisplayName(7)]
        public bool July
        {
            get { return _July; }
            set { _July = value; OnMonthChanged(7, value); }
        }

        private bool _August = true;

        [MonthDisplayName(8)]
        public bool August
        {
            get { return _August; }
            set { _August = value; OnMonthChanged(8, value); }
        }

        private bool _September = true;

        [MonthDisplayName(9)]
        public bool September
        {
            get { return _September; }
            set { _September = value; OnMonthChanged(9, value); }
        }

        private bool _October = true;

        [MonthDisplayName(10)]
        public bool October
        {
            get { return _October; }
            set { _October = value; OnMonthChanged(10, value); }
        }

        private bool _November = true;

        [MonthDisplayName(11)]
        public bool November
        {
            get { return _November; }
            set { _November = value; OnMonthChanged(11, value); }
        }

        private bool _December = true;

        [MonthDisplayName(12)]
        public bool December
        {
            get { return _December; }
            set { _December = value; OnMonthChanged(12, value); }
        }

    }
    public class MonthCheckedChangedEventArgs : EventArgs
    {
        public MonthCheckedChangedEventArgs(int Month, bool Checked)
        {
            this.Month = Month;
            this.Checked = Checked;
        }

        public int Month
        {
            get;
            private set;
        }
        public bool Checked
        {
            get;
            private set;
        }
    }
}
